﻿/**********************************************************************
 * * 版权所有：@ Getlayer 
 * * 类 名 称： 
 * * 功    能： 
 * * 作    者：Getlayer
 * * 电子邮箱：2854989822@qq.com
 * * 创建日期：2014-1-29 19:08:38
 **********************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using System.Data.Common;

namespace Getlayer.DBUtility
{
    public static class ConfigHelper
    {
        /// <summary>
        /// 返回App.Cofig的ConnectionString节点第一个ConnectionString值
        /// </summary>
        /// <returns></returns>
        public static string GetFirstConnectionString()
        {

            if (ConfigurationManager.ConnectionStrings.Count > 0)
            {
                return ConfigurationManager.ConnectionStrings[1].ConnectionString;
            }
            else
                return string.Empty;
        }

        /// <summary>
        /// 返回App.Cofig的ConnectionString节点泛型Dictionary<string providerName, string connecionString>
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetConnctionStringCollection()
        {
            Dictionary<string, string> connectionStrCollect = new Dictionary<string, string>();
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringsSection connectStrSection = config.ConnectionStrings;
            //从i=1开始取值
            for (int i = 1; i < ConfigurationManager.ConnectionStrings.Count; i++)
            {
                ConnectionStringSettings cs = connectStrSection.ConnectionStrings[i];
                connectionStrCollect.Add(cs.ProviderName, cs.ConnectionString);
            }
            return connectionStrCollect;
        }

        /// <summary>
        /// 返回App.Cofig的appSettings节点第一个value值
        /// </summary>
        /// <returns></returns>
        public static string GetFirstAppSetting()
        {

            if (ConfigurationManager.AppSettings.Count > 0)
            {
                //和ConnectionStrings不同，它是从0开始取值的
                return ConfigurationManager.AppSettings[0].ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 返回App.Cofig的appSettings节点泛型Dictionary<string providerName, string connecionString>
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> GetAppSettinCollection()
        {
            Dictionary<string, string> appSettingCollection = new Dictionary<string, string>();
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
            {
                appSettingCollection.Add(appSettings.GetKey(i), appSettings[i]);
            }
            return appSettingCollection;
        }

    }
}


   

